Bellman Equation

We now introduce the Bellman equation, a mathematical tool for analyzing state values.

In a nutshell 简而言之, the Bellman equation is a set of linear equations that describe the relationships between the values of all the states. State values

We next derive the Bellman equation. First, note that Gt can be rewritten as

Gt=Rt+1+γRt+2+γ2Rt+3+=Rt+1+γ(Rt+2+γRt+3+)=Rt+1+γGt+1,

where Gt+1=Rt+2+γRt+3+ . This equation establishes the relationship between Gt and Gt+1 . Then, the state value can be written as

vπ(s)=E[Gt|St=s]=E[Rt+1+γGt+1|St=s]=E[Rt+1|St=s]+γE[Gt+1|St=s].(2.4)

The two terms in (2.4) are analyzed below.
可以分为两项,第一项是现在,第二项是加上折扣因子的未来

The first term, E[Rt+1|St=s] , is the expectation of the immediate rewards. By using the law of total expectation (Appendix A), it can be calculated as
策略 π 下,采取动作 a 的概率

E[Rt+1|St=s]=aAπ(a|s)E[Rt+1|St=s,At=a]=aAπ(a|s)rRp(r|s,a)r.(2.6)

Here, A and R are the sets of possible actions and rewards, respectively. It should be noted that A may be different for different states. In this case, A should be written as A(s) . Similarly, R may also depend on (s,a) . We drop the dependence on s or (s,a) for the sake of simplicity in this book. Nevertheless, the conclusions are still valid in the presence of dependence.

The second term, E[Gt+1|St=s] , is the expectation of the future rewards. It can be calculated as

状态 s 下,下一步为 s 的概率乘以下一步为 sdiscounted return的期望

E[Gt+1|St=s]=sSE[Gt+1|St=s,St+1=s]p(s|s)=sSE[Gt+1|St+1=s]p(s|s)(due to the Markov property)=sSvπ(s)p(s|s)=sSvπ(s)aAp(s|s,a)π(a|s).(2.6)

The above derivation uses the fact that E[Gt+1|St=s,St+1=s]=E[Gt+1|St+1=s], which is due to the Markov property that the future rewards depend merely on the present state rather than the previous ones.

Substituting (2.5)-(2.6) into (2.4) yields

vπ(s)=E[Rt+1|St=s]+γE[Gt+1|St=s],=aAπ(a|s)rRp(r|s,a)rmean of immediate rewards+γaAπ(a|s)sSp(s|s,a)vπ(s)mean of future rewards=aAπ(a|s)[rRp(r|s,a)r+γsSp(s|s,a)vπ(s)],for all sS.(2.7)

This equation is the Bellman equation, which characterizes the relationships of state values. It is a fundamental tool for designing and analyzing reinforcement learning algorithms.

The Bellman equation seems complex at first glance. In fact, it has a clear structure.
Some remarks are given below.

In addition to the expression in (2.7), readers may also encounter other expressions of the Bellman equation in the literature. We next introduce two equivalent expressions.
First, it follows from the law of total probability that

p(s|s,a)=rRp(s,r|s,a),p(r|s,a)=sSp(s,r|s,a).

Then, equation (2.7) can be rewritten as

vπ(s)=aAπ(a|s)sSrRp(s,r|s,a)[r+γvπ(s)].

Second, the reward r may depend solely on the next state s in some problems. As a result, we can write the reward as r(s) and hence p(r(s))|s,a)=p(s|s,a) , substituting which into (2.7) gives

vπ(s)=aAπ(a|s)sSp(s|s,a)[r(s)+γvπ(s)].

2.5 Examples for illustrating the Bellman equation

We next use two examples to demonstrate how to write out the Bellman equation and calculate the state values step by step. Readers are advised to carefully go through the examples to gain a better understanding of the Bellman equation.

Figure 2.4: An example for demonstrating the Bellman equation. The policy in this example is deterministic.

Consider the first example shown in Figure 2.4, where the policy is deterministic. We next write out the Bellman equation and then solve the state values from it.

First, consider state s1 . Under the policy, the probabilities of taking the actions are π(a=a3|s1)=1 and π(aa3|s1)=0 . The state transition probabilities are p(s=s3|s1,a3)=1 and p(ss3|s1,a3)=0 . The reward probabilities are p(r=0|s1,a3)=1 and p(r0|s1,a3)=0 . Substituting these values into (2.7) gives

vπ(s1)=0+γvπ(s3).

Interestingly, although the expression of the Bellman equation in (2.7) seems complex, the expression for this specific state is very simple.

Similarly, it can be obtained that

vπ(s2)=1+γvπ(s4),vπ(s3)=1+γvπ(s4),vπ(s4)=1+γvπ(s4).

We can solve the state values from these equations. Since the equations are simple, we can manually solve them. More complicated equations can be solved by the algorithms presented in Section 2.7. Here, the state values can be solved as

vπ(s4)=11γ,$$$$vπ(s3)=11γ,$$$$vπ(s2)=11γ,$$$$vπ(s1)=γ1γ.

Furthermore, if we set γ=0.9 , then

vπ(s4)=110.9=10,$$$$vπ(s3)=110.9=10,$$$$vπ(s2)=110.9=10,$$$$vπ(s1)=0.910.9=9.

Figure 2.5: An example for demonstrating the Bellman equation. The policy in this example is stochastic.

Consider the second example shown in Figure 2.5, where the policy is stochastic. We next write out the Bellman equation and then solve the state values from it.

In state s1 , the probabilities of going right and down equal 0.5. Mathematically, we have π(a=a2|s1)=0.5 and π(a=a3|s1)=0.5 . The state transition probability is deterministic since p(s=s3|s1,a3)=1 and p(s=s2|s1,a2)=1 . The reward probability is also deterministic since p(r=0|s1,a3)=1 and p(r=1|s1,a2)=1 . Substituting these values into (2.7) gives

vπ(s1)=0.5[0+γvπ(s3)]+0.5[1+γvπ(s2)].

Similarly, it can be obtained that

vπ(s2)=1+γvπ(s4),$$$$vπ(s3)=1+γvπ(s4),$$$$vπ(s4)=1+γvπ(s4).

The state values can be solved from the above equations. Since the equations are

simple, we can solve the state values manually and obtain

vπ(s4)=11γ,$$$$vπ(s3)=11γ,$$$$vπ(s2)=11γ,$$$$vπ(s1)=0.5[0+γvπ(s3)]+0.5[1+γvπ(s2)],$$$$=0.5+γ1γ.

Furthermore, if we set γ=0.9 , then

vπ(s4)=10,$$$$vπ(s3)=10,$$$$vπ(s2)=10,$$$$vπ(s1)=0.5+9=8.5.

If we compare the state values of the two policies in the above examples, it can be seen that

vπ1(si)vπ2(si),i=1,2,3,4,

which indicates that the policy in Figure 2.4 is better because it has greater state values. This mathematical conclusion is consistent with the intuition that the first policy is better because it can avoid entering the forbidden area when the agent starts from s1 . As a result, the above two examples demonstrate that state values can be used to evaluate policies.